// pages/cultureCoupons/couponsTopicPage/index.js
const common = require('../../../public.js')
const utils = require('../../../utils/utils.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cityId: '',
    regionId: '',
    provinceId: '',
    imageId: '',
    tabList: [],
    benefitListData: [],
    isNoMoreData: false,
    tabActiveIndex: 0,
    from: 0,
    lon: '',
    lat: '',
    isLoadingBusy: false,
    storeId: '',
    hideCouponsTool: false,
    userInfo: null,
    realname: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 这里需要处理从实名页面返回调用onload方法没有参数的问题
    let imageId = ''
    const provinceId = options && options.provinceId ? options.provinceId : ''
    if (!common.isEmptyString(provinceId)) {
      this.data.provinceId = provinceId
      imageId = provinceId
    } else {
      const cityId = options && options.cityId ? options.cityId : ''
      const regionId = options && options.regionId ? options.regionId : ''

      if (!common.isEmptyString(cityId)) {
        this.data.cityId = cityId
        imageId = cityId
      }

      if (!common.isEmptyString(regionId) && utils.containRegion(regionId)) {
        this.data.regionId = regionId
        imageId = regionId
      }
    }

    if (!common.isEmptyString(imageId)) {
      this.setData({
        imageId
      })
    }

    getApp().getLocation((data) => {
      this.data.lon = data.longitude
      this.data.lat = data.latitude
      this.getStoreList(true)
    }, () => {
      common.showToast('获取定位信息失败')
    })

    // this.getGoodsType()
    const cityName = this.getTopicTitle()
    if (cityName) {
      wx.setNavigationBarTitle({
        title: '第六届' + cityName + '惠民消费季',
      })
    }
  },

  onShow() {
    const that = this
    wx.getStorage({
      key: 'user',
      success: function (res) {
        that.setData({
          userInfo: res.data.user,
          token: res.data.token,
          realname: res.data.user.isRealname == 1
        })
      },
      fail: function () {
        that.setData({
          userInfo: null
        })
      }
    })
  },

  //获取商品分类
  // getGoodsType() {
  //   var that = this
  //   let tempJson = {
  //     type: 15
  //   }
  //   common.wxRequest("/store/getStoreTypeList ", "post", tempJson, 'application/x-www-form-urlencoded', function (res) {
  //     that.setData({
  //       tabList: res.data,
  //     })
  //   })
  // },

  getStoreList() {



    const param = {
      xpoint: this.data.lon,
      ypoint: this.data.lat
    }
    common.post('/store/getBenefitsPeopleScenicListForLiaoCheng', param, (res) => {
      if (res.status === 200) {

        this.setData({
          benefitListData: res.data
        })

      } else {

        common.showRequestErrMsg(res)
      }
    }, () => {
      this.setData({
        refreshing: false,
        isLoadingBusy: false
      })
    })
  },

  storeListClick(e) {
    common.gotoStore(e.currentTarget.dataset.item)
  },

  loadMore(e) {
    this.getStoreList(false)
  },
  getJumpParma() {
    let tmpList = []

    if (!common.isEmptyString(this.data.provinceId)) tmpList.push('provinceId=' + this.data.provinceId)
    if (!common.isEmptyString(this.data.cityId)) tmpList.push('cityId=' + this.data.cityId)
    if (!common.isEmptyString(this.data.regionId)) tmpList.push('regionId=' + this.data.regionId)

    return tmpList.join('&')
  },
  gotoCouponsCenter() {
    const url = '/pages/cultureCoupons/receiveCoupon/index?' + this.getJumpParma()
    console.log(url)
    wx.navigateTo({
      url
    })
  },
  scan() {
    wx.scanCode({
      success: (res) => {
          const result = res.result
          console.log(result)
          //1. 扫码支付
          if (result && result.indexOf('/pay/scanForPay') != -1) {
              const quary = result.split('?')[1]
              wx.navigateTo({
                  url: '/pages/scanForPay/index?' + quary,
              })
          } else {
              common.showToast('暂不支持当前功能~')
          }
      },
      fail: (res) => {
          if (res.errMse != 'scanCode:fail cancel') {
              common.showToast('扫码失败')
          }
      }
  })
  },
  toRealName() {
    // if (this.data.cityId == 175) {
    //   wx.navigateTo({
    //     url: '/pages/cultureCoupons/discoverForCity/index?cityId=175',
    //   })
    // } else {
    //   wx.redirectTo({
    //     url: '/pages/discovery/index',
    //   })
    // }

    if (this.data.userInfo === null) {
      // 未登录
      common.showLoginPage()
    } else if (this.data.userInfo.isRealname != 1) {
      // 未实名
      common.toRealName()
    } else {
      common.showToast('您已实名')
    }
  },
  gotoCouponsList() {
    wx.navigateTo({
      url: '/pages/cultureCoupons/myCultureCoupons/index',
    })
  },

  getTopicTitle() {
    return utils.getTopicTitle(!common.isEmptyString(this.data.provinceId) ? this.data.provinceId : this.data.cityId, this.data.regionId)
  },
  onShareAppMessage() {
    return {
      title: '第六届' + this.getTopicTitle() + '惠民消费季',
      path: '/pages/cultureCoupons/couponsTopicPage/index?' + this.getJumpParma()
    }
  },
  onShareTimeline() {
    const query = 'from=timeline&' + this.getJumpParma()
    return {
      title: '第六届' + this.getTopicTitle() + '惠民消费季',
      query
    }
  }
})